
# -*- coding:utf-8 -*-
"""
@file: mydict_test.py
@Description:  测试 mydict.py模块中的Dict类
@author: Yu Wenjie
@email : yuwenjie@cetc-cloud.com
@version: 
@date: 2017/10/18 18:34
"""
#测试需要unittest模块（单元测试模块）,继承自unittest.TestCase
#以test开头的方法就是测试方法

import unittest
from mydict import Dict


class TestDict(unittest.TestCase):

    def setUp(self):
        print 'setUp...比如连接数据库'

    def tearDown(self):
        print 'tearDown...关闭数据库'

    def test_init(self):
        d = Dict(a=1, b='test')
        self.assertEquals(d.a, 1)
        self.assertEquals(d.b, 'test')
        self.assertTrue(isinstance(d, dict))

    def test_key(self):
        d = Dict()
        d['key'] = 'value'
        self.assertEquals(d.key, 'value')

    def test_attr(self):
        d = Dict()
        d.key = 'value'
        self.assertTrue('key' in d)
        self.assertEquals(d['key'], 'value')

    def test_keyerror(self):
        d = Dict()
        with self.assertRaises(KeyError):
            value = d['empty']

    def test_attrerror(self):
        d = Dict()
        with self.assertRaises(AttributeError):
            value = d.empty

#运行单元测试脚本的方法有多种，交互式方式： python -m unittest mydict_test
if __name__=="__main__":
    unittest.main()